package net.bw.realtime.jtp.controller;

import com.alibaba.fastjson.JSON;
import net.bw.realtime.jtp.bean.TrafficBrandPvReport;
import net.bw.realtime.jtp.bean.TrafficKeywordReport;
import net.bw.realtime.jtp.bean.TrafficProvinceReport;
import net.bw.realtime.jtp.bean.TrafficUvReport;
import net.bw.realtime.jtp.service.TrafficBrandStatsService;
import net.bw.realtime.jtp.service.TrafficChannelStatsService;
import net.bw.realtime.jtp.service.TrafficKeywordStatsService;
import net.bw.realtime.jtp.service.TrafficProvinceStatsService;
import net.bw.realtime.jtp.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author liuyawei
 * @date 2025/5/21
 */
@RestController
@RequestMapping("/jtp/traffic")
public class TrafficController {

    private static final String DATE_DEFAULT_VALUE = "1970-07-01";

    // 获取uv
    @Autowired
    TrafficChannelStatsService trafficChannelStatsService;
    @RequestMapping("/uvCount")
    public String getUvCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date) {

        // 1.获取参数
        if(DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtils.nowDate();
        }

        // 2.调用service
        List<TrafficUvReport> trafficUvReportList = trafficChannelStatsService.getUvCount(date);

        // 3.判断是否有数据
        if(null ==  trafficUvReportList || trafficUvReportList.isEmpty()){
            return "[]";
        }

        // 4.返回数据
        return JSON.toJSONString(trafficUvReportList);
    }

    // 获取搜索关键词次数
    @Autowired
    TrafficKeywordStatsService trafficKeywordStatsService;
    @RequestMapping("/keywordCount")
    public String getKeywordCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date) {

        // 1.获取参数
        if(DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtils.nowDate();
        }

        // 2.调用service
        List<TrafficKeywordReport> trafficKeywordReports = trafficKeywordStatsService.selectKeywordCount(date);

        // 3.判断是否有数据
        if(null ==  trafficKeywordReports || trafficKeywordReports.isEmpty()){
            return "[]";
        }

        // 4.返回数据
        return JSON.toJSONString(trafficKeywordReports);
    }

    // 获取品牌pv
    @Autowired
    TrafficBrandStatsService trafficBrandStatsService;
    @RequestMapping("/brandPvCount")
    public String getBrandPvCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date) {

        // 1.获取参数
        if(DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtils.nowDate();
        }

        // 2.调用service
        List<TrafficBrandPvReport> trafficBrandPvReports = trafficBrandStatsService.selectPvCount(date);

        // 3.判断是否有数据
        if(null ==  trafficBrandPvReports || trafficBrandPvReports.isEmpty()){
            return "[]";
        }

        // 4.返回数据
        return JSON.toJSONString(trafficBrandPvReports);
    }

    // 获取省份数据
    @Autowired
    TrafficProvinceStatsService trafficProvinceStatsService;
    @RequestMapping("/provinceCount")
    public String getProvinceCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date) {

        // 1.获取参数
        if(DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtils.nowDate();
        }

        // 2.调用service
        List<TrafficProvinceReport> trafficProvinceReports = trafficProvinceStatsService.getProvinceCount(date);

        // 3.判断是否有数据
        if(null ==  trafficProvinceReports || trafficProvinceReports.isEmpty()){
            return "[]";
        }

        // 4.返回数据
        return JSON.toJSONString(trafficProvinceReports);

    }

}
